import 'package:acgn_pioneer/widgets/reader/barrage/slide/barrage_controller.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_barrage.dart';
import 'package:flutter/widgets.dart';
import '../../../public.dart';

/// 弹幕输入框
class ReaderBarrageInput extends StatefulWidget {
  final int comicId;
  final int chapterId;
  final int imgIndex;
  final UserModel userModel;
  final BarrageController barrageController;

  ReaderBarrageInput(
      {Key key,
      this.userModel,
      this.comicId,
      this.chapterId,
      this.imgIndex,
      this.barrageController})
      : super(key: key);

  @override
  ReaderBarrageInputState createState() => ReaderBarrageInputState();
}

class ReaderBarrageInputState extends State<ReaderBarrageInput> {
  final TextEditingController textEditingController = TextEditingController();
  final FocusNode focusNode = FocusNode();
  ReaderBarrageService readerBarrageService = ReaderBarrageService();

  @override
  void initState() {
    super.initState();
    focusNode.requestFocus();
  }

  @override
  void dispose() {
    super.dispose();
    focusNode.dispose();
    textEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 750,
        padding: EdgeInsets.only(top: 10, right: 10, bottom: 10, left: 16),
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Colors.black, width: 1))),
        child: Row(children: [
          Container(
              width: 320,
              margin: EdgeInsets.only(right: 6),
              padding: EdgeInsets.only(left: 16),
              decoration: BoxDecoration(
                  color: ColorUtil.rgba(40, 40, 56, 1),
                  borderRadius: BorderRadius.all(Radius.circular(16))),
              child: TextField(
                  focusNode: focusNode,
                  controller: textEditingController,
                  cursorColor: Colors.red,
                  onSubmitted: (String value) {},
                  onChanged: (String value) {},
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                  ),
                  decoration: InputDecoration(
                    isDense: true,
                    // counterText: '',
                    focusColor: ColorUtil.rgba(255, 255, 255, 0.7),
                    hintText: '弹幕发送准备中（づ￣3￣）づ',
                    hintStyle:
                        TextStyle(color: ColorUtil.rgba(255, 255, 255, 0.3)),
                    errorBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent)),
                    focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent)),
                  ))),
          buildSend()
        ]));
  }

  buildSend() {
    return GestureDetector(
        child: Text('发送', style: TextStyle(color: Colors.white)),
        onTap: () async {
          var user = widget.userModel;
          var model = ChapterBarrageModel(
              comicId: widget.comicId,
              chapterId: widget.chapterId,
              text: textEditingController.text,
              uId: user.uid,
              imageIndex: widget.imgIndex,
              fontColor: 'ffffff', // 传入值没有#号
              isVIP: 0);
          // bool re = await readerBarrageService.addBarrage(model);
          // if (re) {
          widget.barrageController.saveModel([model]);
          // }
        });
  }
}
